package com.groupseven.backend.controller;

import com.groupseven.backend.constant.UserConstants;
import com.groupseven.backend.domain.ResultType;
import com.groupseven.backend.domain.SysDictType;
import com.groupseven.backend.domain.SysDictTypetest;
import com.groupseven.backend.services.ISysDictDataService;
import com.groupseven.backend.services.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典信息
 *
 */
@RestController
@RequestMapping("/api/v1/dict/type")
public class SysDictTypeController extends BaseController
{
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ISysDictDataService dictDataService;

    @GetMapping("/getall")
    public ResultType getall(){
        List<SysDictType> list = dictTypeService.selectDictTypeAll();
        Map<String,Object> map = new HashMap<>();
        List<SysDictTypetest> sysDictTypetestList = new ArrayList<>();
        for(SysDictType sysDictType:list){
            SysDictTypetest sysDictTypetest = new SysDictTypetest();
            sysDictTypetest.setSysDictData(dictTypeService.selectDictDataByType(sysDictType.getDictType()));
            sysDictTypetestList.add(sysDictTypetest);
            map.put(sysDictType.getDictType(),sysDictTypetest.getSysDictData());
        }
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setSysDictTypetestList(sysDictTypetestList);
        resultType.setDicts(map);
        return resultType;
    }


    @GetMapping("/list")
    public ResultType list(SysDictType dictType)
    {
        startPage();//????????????
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setSysDictTypes(list);
        return resultType;
    }


    /**
     * 查询字典类型详细
     */
    @GetMapping(value = "/{dictId}")
    public ResultType getInfo(@PathVariable Long dictId)
    {
        dictTypeService.selectDictTypeById(dictId);
        return new ResultType(1,"查询成功",200);
    }

    /**
     * 新增字典类型
     */
    @PostMapping
    public ResultType add(@Validated @RequestBody SysDictType dict)
    {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict)))
        {
            return new ResultType("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.insertDictType(dict);
        return new ResultType(1,"添加成功",200);
    }

    /**
     * 修改字典类型
     */
    @PutMapping
    public ResultType edit(@Validated @RequestBody SysDictType dict)
    {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict)))
        {
            return new ResultType("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.updateDictType(dict);
        return new ResultType(1,"修改成功",200);
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/{dictIds}")
    public ResultType remove(@PathVariable Long[] dictIds)
    {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return new ResultType(1,"删除成功",200);
    }

    /**
     * 刷新字典缓存
     */
    @DeleteMapping("/refreshCache")
    public ResultType refreshCache()
    {
        dictTypeService.resetDictCache();
        return new ResultType(1,"刷新成功",200);
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public ResultType optionselect()
    {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        ResultType resultType=new ResultType(1,"获取成功",200);
        resultType.setSysDictTypes(dictTypes);
        return resultType;
    }
}
